/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import {TurboModuleRegistry} from 'react-native';
import type {WithDefault} from 'react-native/Libraries/Types/CodegenTypes';

/**
 * Vibrate continuously for a period of time at the default intensity of the system.
 */
export interface VibrateTime {
  /** The value is 'time', which triggers the motor vibration according to the specified duration. */
  type: 'time';
  /** The duration of the vibration, in ms */
  duration: number;
}

/**
 * Preset vibration type vibration effect.
 */
export interface VibratePreset {
  /** The value is 'preset', which triggers motor vibration according to preset vibration effect. */
  type: 'preset';
  /** Preset type vibration. */
  effectId: string;
  /** The number of vibration repetitions. */
  count?: number;
  /** The intensity of vibration effect. */
  intensity?: number;
}

/**
 * Custom vibration, vibrate the effect from a haptic file.
 */
export interface VibrateFromFile {
  /** The value is 'file', which triggers motor vibration according to the vibration profile. */
  type: 'file';
  /** The file name and path are located in the src/main/resources/rawfile directory of the native project. */
  fileName: string;
}

/**
 * The attribute of vibration.
 */
export interface VibrateAttribute {
  /** Vibrator id, default is 0. */
  id?: number;
  /** The use of vibration, default is unknown */
  usage: Usage;
}

/**
 * The use of vibration.
 */
type Usage = WithDefault<
  | 'unknown'
  | 'alarm'
  | 'ring'
  | 'notification'
  | 'communication'
  | 'touch'
  | 'media'
  | 'physicalFeedback'
  | 'simulateReality',
  'unknown'
>;

/**
 * Vibrator vibration stop mode.
 */
export enum VibratorStopMode {
  /** Indicates the mode of stopping a one-shot vibration effect. */
  VIBRATOR_STOP_MODE_TIME = 'time',
  /** Indicates the mode of stopping a preset vibration effect. */
  VIBRATOR_STOP_MODE_PRESET = 'preset',
}
export type VibrateEffect = VibratePreset | VibrateTime | VibrateFromFile;

export interface Spec extends TurboModule {
  /** 查询是否支持传入参数effectId。使用promise异步回调 */
  isSupportEffect(effectId: string): Promise<boolean>;

  /** 根据指定的振动效果和振动属性触发马达振动 */
  startVibration(
    effect: VibrateEffect,
    attribute: VibrateAttribute,
  ): Promise<void>;

  /** 按照指定模式停止马达的振动。使用promise异步回调 */
  stopVibration(stopMode?: VibratorStopMode): Promise<void>;

  /** 停止任何形式的马达振动 */
  stopVibrationSync(): void;
}

export default TurboModuleRegistry.get<Spec>('VibratorModule') as Spec | null;
